package io.xccit.oshiapp.util;

import oshi.SystemInfo;
import oshi.hardware.*;

import java.util.List;

/**
 * @author CH_ywx
 * @version 1.0
 * @create 2024/12/17
 * <p>OSHI工具类</p>
 */
public class OshiUtil {

    private static final SystemInfo systemInfo = new SystemInfo();
    private static final HardwareAbstractionLayer hardware = systemInfo.getHardware();

    /**
     * 获取CPU信息
     *
     * @return CPU信息字符串
     */
    public static String getCpuInfo() {
        CentralProcessor processor = hardware.getProcessor();
        StringBuilder cpuInfo = new StringBuilder();
        cpuInfo.append("CPU: ").append(processor.getProcessorIdentifier().getName()).append("\n")
                .append("Physical Packages: ").append(processor.getPhysicalPackageCount()).append("\n")
                .append("Physical Cores: ").append(processor.getPhysicalProcessorCount()).append("\n")
                .append("Logical Processors: ").append(processor.getLogicalProcessorCount()).append("\n");
        return cpuInfo.toString();
    }

    /**
     * 获取cpu线程每秒使用率
     *
     * @return 每个CPU核心的使用率数组
     */
    public static double[] getCpuCoreUsagePerSecond() {
        CentralProcessor processor = hardware.getProcessor();
        double[] currentLoad = processor.getProcessorCpuLoad(1000);
        return currentLoad;
    }

    /**
     * 获取内存信息
     *
     * @return 内存信息字符串
     */
    public static String getMemoryInfo() {
        GlobalMemory memory = hardware.getMemory();
        StringBuilder memoryInfo = new StringBuilder();
        memoryInfo.append("Total Memory: ").append(memory.getTotal()).append(" bytes\n")
                .append("Available Memory: ").append(memory.getAvailable()).append(" bytes\n")
                .append("Used Memory: ").append(memory.getTotal() - memory.getAvailable()).append(" bytes\n");
        return memoryInfo.toString();
    }

    /**
     * 获取磁盘信息
     *
     * @return 磁盘信息字符串
     */
    public static String getDiskInfo() {
        List<HWDiskStore> storages = hardware.getDiskStores();
        StringBuilder diskInfo = new StringBuilder();
        for (HWDiskStore storage : storages) {
            diskInfo.append("Storage: ").append(storage.getName()).append("\n")
                    .append("Model: ").append(storage.getModel()).append("\n")
                    .append("Serial Number: ").append(storage.getSerial()).append("\n")
                    .append("Size: ").append(storage.getSize()).append(" bytes\n")
                    .append("Read Speed: ").append(storage.getReads()).append(" bytes/s\n")
                    .append("Write Speed: ").append(storage.getWrites()).append(" bytes/s\n")
                    .append("Partitions:\n");
            for (HWPartition partition : storage.getPartitions()) {
                diskInfo.append("  Partition: ").append(partition.getMountPoint()).append("\n")
                        .append("  Size: ").append(partition.getSize()).append(" bytes\n")
                        .append("  Used Space: ").append(partition.getMajor()).append(" bytes\n")
                        .append("  Free Space: ").append(partition.getMinor()).append(" bytes\n");
            }
        }
        return diskInfo.toString();
    }
}
